package pikes.peak;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import pikes.html.xhtml.Block;
import pikes.html.xhtml.Inline;
import pikes.util.Assert;
import org.springframework.web.servlet.support.RequestContext;

public class Paragraph implements BlockComponent {

	private List<InlineComponent> inlineComponents = new LinkedList<InlineComponent>();

	private TextGroupStyle style = TextGroupStyle.PARAGRAPH;

	private CharSequence clazz = null;

	public Paragraph(String text) {
		this(text, TextGroupStyle.PARAGRAPH);
	}

	public Paragraph(String text, TextGroupStyle level) {
		Assert.hasText(text);
		Assert.notNull(level);
		add(new Text(text));
		this.style = level;
	}

	public Paragraph(InlineComponent text, TextGroupStyle level) {
		Assert.notNull(text);
		Assert.notNull(level);
		add(text);
		this.style = level;
	}

	public Paragraph(InlineComponent text) {
		Assert.notNull(text);
		add(text);
	}

	public Paragraph() {

	}

	public Paragraph(TextGroupStyle level) {
		Assert.notNull(level);
		this.style = level;
	}

	public Block createHtml(Map model, RequestContext requestContext) throws Exception {
		List<Inline> inlines = new LinkedList<Inline>();
		for (InlineComponent inline : inlineComponents) {
			inlines.add(inline.createHtml(model, requestContext));
		}
		if (clazz != null) {
			return style.apply(inlines, clazz);
		}
		else {
			return style.apply(inlines);
		}

	}

	public void add(String text) {
		add(new Label(text));
	}

	public void add(InlineComponent text) {
		if (text != null) {
			inlineComponents.add(text);
		}

	}

	public CharSequence getClazz() {
		return clazz;
	}

	public void setClazz(CharSequence clazz) {
		this.clazz = clazz;
	}
}
